import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'
import { Toast } from 'vant'
const routes = [{
        path: '/',
        name: '/',
        redirect: '/main/found'
    },
    {
        path: '/main',
        name: 'main',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Main.vue'),
        children: [{
                path: 'found',
                name: 'found',
                component: () =>
                    import ('../views/Found.vue'),
                meta: { index: 1 }
            },
            {
                path: 'mine',
                name: 'mine',
                component: () =>
                    import ('../views/Mine.vue'),
                meta: { index: 2 }
            }, {
                path: 'attention',
                name: 'attention',
                component: () =>
                    import ('../views/Attention.vue'),
                meta: { index: 3 }
            }

        ]
    },
    {
        path: '/recommendSongs',
        name: 'recommendSongs',
        component: () =>
            import ('../views/RecommendSongs.vue'),
        meta: { index: 5 }
    },
    {
        path: '/songPlayDetail',
        name: 'songPlayDetail',
        component: () =>
            import ('../views/songPlayDetail.vue'),
        meta: { index: 6 }
    },
    {
        path: '/search',
        name: 'search',
        component: () =>
            import ('../views/Search.vue'),
        meta: { index: 7 }
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('../views/Login.vue'),
        meta: { index: 8 }
    },
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ('../views/Register.vue'),
        meta: { index: 9 }
    },
    {
        path: '/setting',
        name: 'Setting',
        component: () =>
            import ('../views/Setting.vue'),
        meta: { index: 11, hasLogin: true }
    },
    {
        path: '/comment',
        name: 'comment',
        component: () =>
            import ('../views/Comment.vue'),
        meta: { index: 12 }
    },
    {
        path: '/tousu',
        name: 'tousu',
        component: () =>
            import ('../views/tousu.vue'),
        meta: { index: 13 }
    },



]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//前置路由守卫：评论页面必须登录
router.beforeEach((to, from) => {
    if (to.meta.hasLogin === true) {
        if (!store.state.userInfo) {
            Toast('请登录');
            return to.path = { path: '/login', query: { redirect: to.fullPath } }
        }
    }
})

export default router